package com.yyk.yiliao.ui.mine.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.bean.MymessageVisideInfo;
import com.yyk.yiliao.bean.MymessageVisitlookInfo;
import com.yyk.yiliao.listener.DialogSureCancelListenter;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.TimeUtil;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.DialogUtil;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 查询 关系信息
 */
public class QueryMember_Activity extends BaseActivity {


    @BindView(R.id.tv_one)
    TextView tvOne;

    @BindView(R.id.rl_one)
    RelativeLayout rlOne;
    @BindView(R.id.tv_two)
    EditText tvTwo;
    @BindView(R.id.rl_two)
    RelativeLayout rlTwo;
    @BindView(R.id.tv_four)
    EditText tvFour;
    @BindView(R.id.rl_four)
    RelativeLayout rlFour;
    @BindView(R.id.tv_three)
    EditText tvThree;
    @BindView(R.id.rl_three)
    RelativeLayout rlThree;
    @BindView(R.id.tv_five)
    TextView tvFive;
    @BindView(R.id.rl_five)
    RelativeLayout rlFive;
    @BindView(R.id.tv_six)
    TextView tvSix;
    @BindView(R.id.rl_six)
    RelativeLayout rlSix;
    @BindView(R.id.tv_seven)
    EditText tvSeven;
    @BindView(R.id.rl_seven)
    RelativeLayout rlSeven;
    @BindView(R.id.tv_eight)
    EditText tvEight;
    @BindView(R.id.rl_eight)
    RelativeLayout rlEight;
    @BindView(R.id.ll_wu)
    LinearLayout llWu;
    @BindView(R.id.bt_detele)
    Button btDetele;

    private String patient_id;
    private String status;
    private Map<String, String> map;


    @Override
    public int getLayoutId() {
        return R.layout.activity_query_member;
    }

    @Override
    public void initView() {
        initToobar();
    }

    private void initToobar() {
        setBackArrow();
        setTitle("就诊成员");
        //
//        setMainTitleRightText("修改", new ToolbarListenter() {
//            @Override
//            public void onClickListenter() {
//                Intent intent = new Intent(QueryMember_Activity.this, UpadteMember_Activity.class);
//                startActivity(intent);
//            }
//        });
    }


    @Override
    public void initData() {
        initGetIntent();
        initPost();

    }

    /**
     * 个人信息
     */
    private void initPost() {
        RxUtils.getInstance().retrofit.create(ApiService.class).postMymessageVisitlook(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<MymessageVisitlookInfo>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(MymessageVisitlookInfo info) {
                        Logger.e("人员选择" + info.toString());
                        if (info.getCode() == 1) {
                            MymessageVisitlookInfo.DataBean data = info.getData();
                            String real_name = data.getReal_name(); //本人姓名
                            String patient_phone = data.getPatient_phone(); //手机号
                            int sex = data.getSex(); //性别
                            String noidcard_user = data.getNoidcard_user(); //看护人姓名
                            String noidcard_card = data.getNoidcard_card();//身份证卡号
                            int birth = data.getBirth();//出生日期
                            String csrq = TimeUtil.getInstance().getTime(birth, TimeUtil.YYYY_MM_DD_TEXT);

                            if (data.getType() == 1) {
                                tvOne.setText("本人");
                            } else if (data.getType() == 2) {
                                tvOne.setText("父母");
                            } else if (data.getType() == 3) {
                                tvOne.setText("父亲");
                            } else if (data.getType() == 4) {
                                tvOne.setText("子女(已领取身份证)");
                            } else if (data.getType() == 5) {
                                tvOne.setText("子女(未领取身份证)");
                            } else {
                                tvOne.setText("其他");
                            }
                            tvTwo.setText(real_name);  //姓名
                            tvFour.setText(patient_phone);
                            tvThree.setText(noidcard_card);

                            tvFive.setText(csrq);
                            //设置性别
                            tvSix.setText(data.getSex() == 1 ? "男" : "女");
                            //设置看护人姓名
                            tvSeven.setText(noidcard_user);
                            tvEight.setText(noidcard_card);


                        }

                    }

                });

    }

    private void initGetIntent() {
        if (getBundle() != null) {
            patient_id = getBundle().getString("patient_id");
            status = getBundle().getString("status");
            if (status.equals("1")) {
                llWu.setVisibility(View.GONE);
                rlThree.setVisibility(View.VISIBLE);
            } else {
                llWu.setVisibility(View.VISIBLE);
                rlThree.setVisibility(View.GONE);
            }
            map = new TreeMap<>();
            map.put("patient_id", patient_id);
            map.put("status", status);

            map.put("sign", Md5Util2.createSign(map));
        }
    }

    /**
     * 删除具体患者
     */
    private void initDelete() {
        RxUtils.getInstance().retrofit.create(ApiService.class).postMymessageViside(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<MymessageVisideInfo>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(MymessageVisideInfo info) {
                        Logger.e("删除具体成员" + info.toString());
                        if (info.getCode() == 1) {
                            ToastUtil.showShort(mActivity, "删除成功");
                            finish();

                        } else {
                            ToastUtil.showShort(mActivity, "删除失败");
                        }

                    }

                });

    }


    private void showNormalDialog() {
        DialogUtil.getIntanse().showReturnTips(mActivity, null, "您确定要删除成员吗?", "确定", "取消", new DialogSureCancelListenter() {
            @Override
            public void onSureListenter() {
                initDelete();
            }

            @Override
            public void onCancelListenter() {

            }
        }, false);

    }

    @OnClick({R.id.bt_detele})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_detele:
                showNormalDialog();
                break;
        }
    }


}
